<?php
/**
 * 退卡记录
 *
 * @author comfiger<liuqiang@wisdudu.com>
 * @version v1.0.0
 *
 **/
namespace Xijie\Controller;
use Think\Controller;

class CardlogController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	public function index(){
		if(IS_GET){
			$page = I('get.page') ? (int)I('get.page') : 1;
			$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
			
			//获取时间或者类型
			$type_id = I('get.type_id') ? (int)I('get.type_id') : 0;
			$btime = I('get.btime') ? trim(I('get.btime')) : '';
			$etime = I('get.etime') ? trim(I('get.etime')) : '';
			
			$where = array('cardlog.visible' => 1);
			
			//付款类型 1：取卡 2：续费
			$where['cardlog.action_id'] = 2;
			
			//设置查询时间
			if(!empty($btime) && !empty($etime)){
				$btime = $btime. '00:00:00';
				$etime = $etime. '23:59:59';
				$where['cardlog.createtime'] = array('between', array(strtotime($btime), strtotime($etime)));
			}
			
			$count = D('Cardlog')->getCardlogCount($where);
			//支付列表
			$list = D('Cardlog')->getCardlogList($page, $where);
			
			$this->assign('list', $list);
			$this->assign('count', $count);
			$this->assign('page', $page);
			//设置查询参数
			$this->assign('param', json_encode($where));
			//判断请求方式
			if(!$ajax){
				$this->display('cardlog');
			}else{
				$this->display('cardlog_list', 'utf-8', 'html');
			}
		}
	}
	
	/**
	 * 售卡记录
	 **/
	public function sell(){
		if(IS_GET){
			$page = I('get.page', 1);
			$ajax = I('get.ajax', 0);
			
			$where = array(
				'typeid' => 4,
				'status' => 1
			);
			//判断是否存在房号
			$roomnumber = I('get.roomnumber', '');
			if(!empty($roomnumber)){
				$where['roomnumber'] = $roomnumber;
			}
			
			$count = D('Gobacklog')->getGobacklogCount($where);
			//列表
			$list = D('Gobacklog')->getGobacklogList($page, $where);
			$this->assign('list', $list);
			$this->assign('count', $count);
			$this->assign('page', $page);
			
			if(!$ajax){
				$this->display();
			}else{
				$this->display('sell_list', 'utf-8', 'html');
			}
		}else{
			$id = I('post.ids', 0);
			$info = D('Gobacklog')->getGobackLogInfoById($id);
			$data = array(
				'log_id' => $id,
				'roomnumber' => $info['roomnumber'],
				'member_id' => $info['userid'],
				'nickname' => $info['nickname'],
				'action_id' => 2,
				'createtime' => time(),
				'user_id' => session('user_id')
			);
			//修改状态
			$row = D('Gobacklog')->editGobackLogStatusById($id, array(
				'is_back' => 1
			));
			if($row !== false){
				$row = D('Cardlog')->addCardlogInfo($data);
				//添加退卡退款日志
				$go_data = array(
					'typeid' => 0,
					'contract' => $info['contract'],
					'userid' => $info['userid'],
					'nickname' => $info['nickname'],
					'roomnumber' => $info['roomnumber'],
					'card_deposit' => -100,
					'acount' => -100,
					'pay_type' => 0,
					'pay_card' => '',
					'createtime' => time(),
					//'remark' => '房卡押金退款',
					'status' => 1,
					'xianjin' => -100,
					'admin_nick' => session('nickname'),
					'remark' => I('post.remark', '')
				);
				
				$row = D('Gobacklog')->addGobacklogInfo($go_data);

				if($row !== false){
					echo 1;
				}else{
					echo 0;
				}
			}else{
				echo 1;
			}
		}
	}
}


   